package com.example.creditscore;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;

public class Credit_score_form_sec_d extends Activity {

	/**
	 * @uml.property  name="btnNextPage"
	 * @uml.associationEnd  
	 */
	private Button BtnNextPage;
	/**
	 * @uml.property  name="btnBackPage"
	 * @uml.associationEnd  
	 */
	private Button btnBackPage;

	private boolean flag = true;
	RadioButton rd;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.credit_score_form_sec_d);
		BtnNextPage = (Button) findViewById(R.id.nextPageBtn);
		btnBackPage = (Button) findViewById(R.id.backPageBtn);
		btnBackPage.setOnClickListener(backPage());
		BtnNextPage.setOnClickListener(nextPage());

		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		StrictMode.setThreadPolicy(policy);

		isNetworkAvailable();
		startLoading();

	}

	private OnClickListener nextPage() {
		final Context context = this;
		return new OnClickListener() {
			public void onClick(View v) {

				Intent intent = new Intent(context,
						Credit_score_form_sec_e.class);
				startActivity(intent);

			}
		};
	}
	
	private OnClickListener backPage() {
		final Context context = this;
		return new OnClickListener() {
			public void onClick(View v) {

				Intent intent = new Intent(context,
						Credit_score_form_sec_c.class);
				startActivity(intent);

			}
		};
	}

	private void isNetworkAvailable()
	{
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netInfo = cm.getActiveNetworkInfo();
		if (netInfo != null && netInfo.isConnectedOrConnecting())
		{
			return;
		}
		else
		{
			// Toast
			Toast msg = Toast.makeText(this, "No Internet Connection Available", Toast.LENGTH_LONG);
			msg.show();
		}
	}

	private void startLoading(){

		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost("http://chandrajcu.webfactional.com/jcumfidatabase/credit_score_section_d.php");

		try
		{	methods md = new methods();
			String clientNumList = md.retriveSharePreference(this, "retriveValue", "dClientListNum");
			Log.d("My Application", "I Am Here");
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
			nameValuePairs.add(new BasicNameValuePair("collectInfoSectionD", clientNumList));
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

			ResponseHandler<String> responseHandler = new BasicResponseHandler();
			String response = httpclient.execute(httppost, responseHandler);

			String[] splits = new String[7];
			splits = response.split(":");

			while((flag) && (splits != null))
			{
				int age, childNum, salary;

				age = Integer.parseInt(splits[0]);

				if ((age > 20) && (age < 51))
				{
					rd = (RadioButton)findViewById(R.id.radioAge1);
					rd.setChecked(true);
				}
				else if((age > 50) && (age < 61))
				{
					rd = (RadioButton)findViewById(R.id.radioAge2);
					rd.setChecked(true);
				}
				else if (age > 60)
				{
					rd = (RadioButton)findViewById(R.id.radioAge3);
					rd.setChecked(true);
				}
				else
				{
					// toast here
				}

				if (splits[1].equals("Married"))
				{
					rd = (RadioButton)findViewById(R.id.radioMS1);
					rd.setChecked(true);
				}
				else if (splits[1].equals("Single"))
				{
					rd = (RadioButton)findViewById(R.id.radioMS2);
					rd.setChecked(true);
				}
				else if (splits[1].equals("Separated"))
				{
					rd = (RadioButton)findViewById(R.id.radioMS3);
					rd.setChecked(true);
				}
				else if (splits[1].equals("Divorced"))
				{
					rd = (RadioButton)findViewById(R.id.radioMS4);
					rd.setChecked(true);
				}
				else if (splits[1].equals("Defacto"))
				{
					rd = (RadioButton)findViewById(R.id.radioMS5);
					rd.setChecked(true);
				}

				childNum = Integer.parseInt(splits[2]);

				if ((childNum >= 0) && (childNum <= 2))
				{
					rd = (RadioButton)findViewById(R.id.radioNoDenpendents1);
					rd.setChecked(true);
				}
				else if (childNum > 2)
				{
					rd = (RadioButton)findViewById(R.id.radioNoDenpendents2);
					rd.setChecked(true);
				}

				if (splits[3].equals("Married"))
				{
					rd = (RadioButton)findViewById(R.id.radioHousing1);
					rd.setChecked(true);
				}
				else if (splits[3].equals("Own Home Mortgaged"))
				{
					rd = (RadioButton)findViewById(R.id.radioHousing2);
					rd.setChecked(true);
				}
				else if (splits[3].equals("Live With Parents"))
				{
					rd = (RadioButton)findViewById(R.id.radioHousing3);
					rd.setChecked(true);
				}
				else if (splits[3].equals("Boarding"))
				{
					rd = (RadioButton)findViewById(R.id.radioHousing4);
					rd.setChecked(true);
				}
				else if (splits[3].equals("Renting"))
				{
					rd = (RadioButton)findViewById(R.id.radioHousing5);
					rd.setChecked(true);
				}
				else if (splits[3].equals("Other"))
				{
					rd = (RadioButton)findViewById(R.id.radioHousing6);
					rd.setChecked(true);
				}

				if (splits[4].equals("Full-time"))
				{
					rd = (RadioButton)findViewById(R.id.radioSocOfIncome1);
					rd.setChecked(true);
				}
				else if (splits[4].equals("Part-time") || splits[4].equals("Casual") || splits[4].equals("Contractor") || splits[4].equals("Self employed") || splits[4].equals("Not employed"))
				{
					rd = (RadioButton)findViewById(R.id.radioSocOfIncome2);
					rd.setChecked(true);
				}

				salary = Integer.parseInt(splits[5]);

				if ((salary >= 500) && (salary <= 1500))
				{
					rd = (RadioButton)findViewById(R.id.radioGrossMthlySalary1);
					rd.setChecked(true);
				}
				else if ((salary >= 1500) && (salary <= 2500))
				{
					rd = (RadioButton)findViewById(R.id.radioGrossMthlySalary2);
					rd.setChecked(true);
				}
				else if ((salary >= 2500) && (salary <= 3500))
				{
					rd = (RadioButton)findViewById(R.id.radioGrossMthlySalary3);
					rd.setChecked(true);
				}
				else if (salary > 3500)
				{
					rd = (RadioButton)findViewById(R.id.radioGrossMthlySalary4);
					rd.setChecked(true);
				}

				if (splits[6].equals("Director"))
				{
					rd = (RadioButton)findViewById(R.id.radioOccupation1);
					rd.setChecked(true);
				}
				else if (splits[6].equals("Manager"))
				{
					rd = (RadioButton)findViewById(R.id.radioOccupation2);
					rd.setChecked(true);
				}
				else if (splits[6].equals("Executive"))
				{
					rd = (RadioButton)findViewById(R.id.radioOccupation3);
					rd.setChecked(true);
				}

				flag = false;
			}

		}
		catch (Exception ex)
		{
			Log.d("My Application", "Exception: " + ex.toString());
		}
	}

}
